/*
 * @Author: czy
 * @Date: 2021-04-04 22:39:40
 * @LastEditTime: 2021-07-12 22:13:29
 */
#include "../lib/io.h"
#include "../lib/uart.h"
#include "../lib/output.h"

/**
 * @description: Clear the screen and return the cursor to its original value.
 */
void uart_clear(void)
{
    outb(uart_base, 033);
    outb(uart_base, 'c');
}

/**
 * @description: Output a character to the uart.
 * @param ch    Character to be output.
 */
void uart_put_char(char ch)
{
    switch (ch)
    {
    case 127:
        outb(uart_base, '\b');
        outb(uart_base, ' ');
        outb(uart_base, '\b');
        break;
    case '\n':
        outb(uart_base, '\n');
        outb(uart_base, '\r');
        break;
    default:
        outb(uart_base, ch);
        break;
    }
}

/**
 * @description: Output a string to the uart.
 * @param str   String to be output.
 */
int uart_put_chars(char *str)
{
    int i = 0;
    do
    {
        uart_put_char(str[i]);
    } while (str[++i] != '\0');
    return i;
}

/**
 * @description: Cursor offset.
 * @param offset    Offset.
 */
void uart_offset(int offset)
{
    if (offset > 0)
    {
        for (int i = 0; i < offset; i++)
        {
            uart_put_char(27);
            uart_put_char(91);
            uart_put_char(67);
        }
    }
    else
    {
        for (int i = 0; i < -offset; i++)
        {
            uart_put_char(27);
            uart_put_char(91);
            uart_put_char(68);
        }
    }
}